package DAO;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import Entita.TavolaPaziente;
import Entita.Paziente;
import Entita.Tavola;
import Entita.TavolaPaziente;
import GUI.Main;

public class LogDAO
	{


		public LogDAO()
		{

		}

		private Connection getConnection() throws SQLException
		{
			Connection conn;
			conn=ConnectionFactory.getInstance().getConnection();
			return conn;
		}


		public static void log()
		{
				
			Connection con=null;
			PreparedStatement ptmt=null;
			ResultSet rs=null;
			try
			{
				String querystring="delete from Log where Accesso<(select max(Accesso) from Log);\ninsert into Log (Accesso) values(date())";
				con=ConnectionFactory.getInstance().getConnection();
				ptmt=con.prepareStatement(querystring);
				ptmt.executeUpdate();
				
				

			}
			catch(SQLException e)
			{
				Main.showError("Errore:"+e.getMessage());
			}
			finally
			{
				try
				{
					if(rs!=null)
						rs.close();
					if(ptmt!=null)
						ptmt.close();
					if(con!=null)
						con.close();
				}
				catch(SQLException e)
				{
					Main.showError("Errore:"+e.getMessage());
				}
				catch(Exception e)
				{
					Main.showError("Errore:"+e.getMessage());
				}

			}

		}


		public static java.util.Date getUltimoAccesso()
		{
			Connection con=null;
			PreparedStatement ptmt=null;
			ResultSet rs=null;
			try
			{
				String querystring="SELECT Max(Accesso) Accesso FROM Log";

				con=ConnectionFactory.getInstance().getConnection();
				ptmt=con.prepareStatement(querystring);
				rs=ptmt.executeQuery();
				if(rs.next())
				{
					return rs.getDate("Accesso");
				}else{
					return null;
				}
					
			}
			catch(SQLException e)
			{
				Main.showError("Errore:"+e.getMessage());
			}
			finally
			{
				try
				{
					if(rs!=null)
						rs.close();
					if(ptmt!=null)
						ptmt.close();
					if(con!=null)
						con.close();
				}
				catch(SQLException e)
				{
					Main.showError("Errore:"+e.getMessage());
				}
				catch(Exception e)
				{
					Main.showError("Errore:"+e.getMessage());
				}

			}
			return null;
		}
		
		
	}